package internationalenergytradingplatform.controller.SupplierController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import internationalenergytradingplatform.VO.OrderPageQueryVO;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.BuyerMapper;
import internationalenergytradingplatform.dao.OrderMapper;
import internationalenergytradingplatform.dao.TransporterAccountMapper;
import internationalenergytradingplatform.dto.OrderPageQueryDTO;
import internationalenergytradingplatform.pojo.*;
import internationalenergytradingplatform.result.PageResult;
import internationalenergytradingplatform.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 名称OrderController
 * <p>
 * 描述订单管理
 *
 * @version 1.0
 * @author:Zuowenbo
 * @datatime:2024-03-19 00:46
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
@Slf4j
public class OrderController {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    TransporterAccountMapper transporterMapper;

    @Autowired
    BuyerMapper buyerMapper;

    /**
     *
     * @author yingfeng
     * @date 2024/3/4 21:40
    订单分页查询
     */
    @GetMapping("/paginationQuery")
    public Result<PageResult> pagingToQueryAllOrder(OrderPageQueryDTO orderPageQueryDTO){
        String companyId = BaseContext.getCurrentId();//获取公司id
        orderPageQueryDTO.setSupplier(companyId);
        PageHelper.startPage(orderPageQueryDTO.getPage(), orderPageQueryDTO.getPageSize());
        Page<OrderPageQueryVO> page = orderMapper.orderPageQuery(orderPageQueryDTO);
        long total = page.getTotal();
        List<OrderPageQueryVO> records = page.getResult();
        log.info("分页查询结果：{}", records);
        PageResult pageResult = new PageResult(total, records);
        return Result.success(pageResult);
    }

    /**
     * 通过采购商和供应商的电话，获取采购商和供应商的全部信息
     */
    @GetMapping("/contactTAndB")
    public Result<Map<String, Object>> contactTAndB(String transporter, String buyer){
        //根据手机号查找transporter的全部信息
        TransporterAccount transporterByTele = transporterMapper.getCompanyByTele(transporter);
        transporterByTele.setPassword(null);
        transporterByTele.setBalance(null);
        //根据手机号查找buyer的全部信息、
        BuyerAccount buyerByTele = buyerMapper.getCompanyByTele(buyer);
        buyerByTele.setPassword(null);
        buyerByTele.setBalance(null);
        Map<String, Object> response = new HashMap<>();
        response.put("transporter", transporterByTele);
        response.put("buyer", buyerByTele);
        return Result.success(response);
    }

    /**
     * 进入下一流程
     */
    @PutMapping("/nextProcess")
    public Result nextProcess(Integer orderId, String imgUrl) {
        Order orderById = orderMapper.getOrderById(orderId);
        if(orderById.getStatus() != 1) {
            return Result.error("你无权将订单进入下一步！");
        } else {
            orderMapper.updateStatus(imgUrl, orderId);
            return Result.success();
        }
    }

}
